using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TradeFlow.Server.Service;

namespace TradeFlow.Web.Controllers
{
    /// <summary>
    /// 库存控制器
    /// </summary>
    [Authorize]
    public class JxStockController : Controller
    {
        private readonly JxStockService _service;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="service"></param>
        public JxStockController(JxStockService service)
        {
            _service = service;
        }

        /// <summary>
        /// 商品库存表页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取商品库存列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetStockList()
        {
            var result = await _service.GetStockListAsync();
            return Json(result);
        }

        ///// <summary>
        ///// 根据商品编码查询库存
        ///// </summary>
        ///// <param name="code"></param>
        ///// <returns></returns>
        //[HttpGet]
        //public async Task<IActionResult> GetStockByCode(string code)
        //{
        //    var result = await _service.GetStockByCodeAsync(code);
        //    return Json(result);
        //}

        /// <summary>
        /// 查询商品库存（分页）
        /// </summary>
        /// <param name="pageNo">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="keyword">关键字（商品编码、名称或型号）</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> QueryInventories(int pageNo = 1, int pageSize = 20, string keyword = null)
        {
            var result = await _service.QueryInventoriesAsync(pageNo, pageSize, keyword);
            return Json(result);
        }

        /// <summary>
        /// 检查库存是否不足
        /// </summary>
        /// <param name="goodsCode"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetStockByGoodsCode(string goodsCode)
        {
            var result = await _service.GetStockByGoodsCodeAsync(goodsCode);
            return Json(result);
        }

        /// <summary>
        /// 检查库存是否充足
        /// </summary>
        /// <param name="goodsCode"></param>
        /// <param name="requiredQuantity"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> CheckStock(string goodsCode, int requiredQuantity)
        {
            var result = await _service.CheckStockAsync(goodsCode, requiredQuantity);
            return Json(result);
        }
    }
}